package com.unytech.wisdomcommunity.complainrepairfragment;

import android.app.Activity;
import android.app.Fragment;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.unytech.wisdomcommunity.R;

/**
 * "投诉报修"的初稿的Activity
 */
public class BaseActivity extends AppCompatActivity{

    private TextView tv_comlain, tv_repair;
    private ImageView comlainOrRepairIv,comlainRepairBackIv;
    private TextView mTitleTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base);

        getFragmentManager()
                .beginTransaction()
                .replace(R.id.demo_base_fragment_container, new ComplainFragment())
                .commit();
        //初始化view
        initView();
    }

    private void initView() {
        mTitleTv = ((TextView) findViewById(R.id.comlainrepair_fragment_base_title_tv));
        mTitleTv.setText("投诉");

        comlainRepairBackIv = ((ImageView) findViewById(R.id.comlainrepair_fragment_base_back_iv));
        comlainRepairBackIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        comlainOrRepairIv = ((ImageView) findViewById(R.id.comlain_and_repair_change_iv));
        comlainOrRepairIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //2.初始化popupWindow
                initPopupWindow(v);
            }
        });
    }
    //替换fragment的方法
    private void replaceFragment(Fragment fragment) {
        getFragmentManager()
                .beginTransaction()
                .replace(R.id.demo_base_fragment_container, fragment)
                .commit();
    }
    //2.初始化popupWindow
    private void initPopupWindow(View v) {
        View pupView = LayoutInflater.from(this).inflate(R.layout.popup_window_comlain_repair_layout, null);

        final PopupWindow popupWindow = new PopupWindow(pupView,
                LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);

        tv_comlain = ((TextView) pupView.findViewById(R.id.pop_tv_complain));
        tv_repair = ((TextView) pupView.findViewById(R.id.pop_tv_repair));

        tv_comlain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                replaceFragment(new ComplainFragment());//替换fragment
                mTitleTv.setText("投诉");
                popupWindow.dismiss();
            }
        });
        tv_repair.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                replaceFragment(new RepairFragment());//替换fragment
                mTitleTv.setText("报修");
                popupWindow.dismiss();
            }
        });

        /*popupWindow.setFocusable(true);
        popupWindow.setTouchable(true);*/
        //获取view 在屏幕上的X和Y的坐标   location[0]表示X轴   location[1]表示Y轴
        int[] location = new int[2];
        v.getLocationOnScreen(location);

        popupWindow.setOutsideTouchable(true);
        //popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        popupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
        //把事件拦截下来
        popupWindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                Log.i("mengdd", "onTouch : ");

                return false;
                // 这里如果返回true的话，touch事件将被拦截
                // 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss
            }
        });

        //backgroundAlpha(this, 0.8f);
        // 0.0-1.0   1.0代表完全透明  0.0代表完全不透明

        //popupWindow.showAsDropDown(v, 50, 100);
        //popupWindow.showAtLocation(v, Gravity.BOTTOM,0,0);
        //TODO 11.19  在1080P的手机上这里Y轴偏移量才是150px
        popupWindow.showAtLocation(comlainOrRepairIv, Gravity.TOP|Gravity.RIGHT,location[0] -1300, location[1]+150);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                //backgroundAlpha(BaseActivity.this, 1f);
                //把这个方法注释掉  解决弹出popupWindow时屏幕闪一下黑屏的问题
            }
        });
    }
    //这个方法可以不用
    private void backgroundAlpha(Activity appContext, float bgAlpha) {
        WindowManager.LayoutParams lp = appContext.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        appContext.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        appContext.getWindow().setAttributes(lp);
    }
}